package com.common;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

/**
 * 自定义百分数TypeHandler
 * 
 * @author：<a href="mailto:xiaodong.zhang@yeepay.com">张晓冬</a>
 * @since：2012-6-15 上午11:30:22
 * @version:
 */
public class PercentTypeHandler implements TypeHandler {

	public void setParameter(PreparedStatement ps, int i, Object parameter,
			JdbcType jdbcType) throws SQLException {
		if (parameter != null) {
			Percent percent = (Percent) parameter;
			ps.setBigDecimal(i, percent.getValue());
		} else {
			ps.setNull(i, Types.DECIMAL);
		}
	}

	public Object getResult(ResultSet rs, String columnName)
			throws SQLException {
		BigDecimal number = rs.getBigDecimal(columnName);
		if (number == null) {
			return null;
		}
		Percent percent = new Percent(number);
		return percent;
	}

	public Object getResult(CallableStatement arg0, int arg1)
			throws SQLException {
		return null;
	}
}
